package cn.doctor.assistant.pad.api.huifang;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.doctor.assistant.pad.api.AssistAPI;
import cn.doctor.assistant.pad.api.bean.AssResponse;
import cn.doctor.assistant.pad.api.bean.ReqBean;
import cn.doctor.assistant.pad.base.AssJsonRequest;
import cn.doctor.assistant.pad.base.ResultCallback;
import cn.doctor.assistant.pad.entity.FollowedBean;
import cn.doctor.assistant.pad.entity.PageInfo;
import cn.doctor.assistant.pad.entity.Recall;

/**
 * Created by jiaolf on 2016/3/24.
 * 回访
 */
public class ReCallApi {

    public static final String METHOD_GET_FollowedByRecord = "GetFollowedByRecord";//根据住院ID获取回访记录列表
    public static final String METHOD_GET_FOLLOW = "GetFollowed";//获取回访记录列表
    public static final String METHOD_Search_Followed = "SearchFollowed"; //查询回访记录
    public static final String METHOD_Add_Followed = "AddFollowed";//新增回访记录

    /**
     * 根据住院ID获取回访记录列表
     * @param record_id
     * @param callback
     */
    public static void getFollowedByRecord(int record_id, final ResultCallback<List<FollowedBean>> callback) {
        Map<String, Object> params = new HashMap<>();
        params.put("record_id", record_id);

        ReqBean reqBean = ReqBean.newInstance(METHOD_GET_FollowedByRecord, params);
        String jsonParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                AssResponse resp = JSON.parseObject(response, AssResponse.class);
                List<FollowedBean> list = JSON.parseArray(resp.getJsonValue("followed"), FollowedBean.class);
                callback.onSuccess(list);
            }
        });

        jsonRequest.setTag(METHOD_GET_FollowedByRecord);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

    /**
     * 获取回访记录列表
     * @param isFollowed 0：已回访， 1：待回访
     * @param callback
     */
    public static void getFollowedList(int isFollowed, PageInfo pageInfo, final ResultCallback<FollowedListResponse> callback) {
        GetFollowedRequest getFollowedRequest = new GetFollowedRequest();
        getFollowedRequest.setIsFollowed(isFollowed);
        getFollowedRequest.setPageInfo(pageInfo);

        ReqBean reqBean = ReqBean.newInstance(METHOD_GET_FOLLOW, getFollowedRequest);
        String jsonParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                AssResponse resp = JSON.parseObject(response, AssResponse.class);
                FollowedListResponse followedListResponse = new FollowedListResponse();
//                followedListResponse.setPageTotal(JSON.parseObject(resp.getJsonValue("pageTotal"), Integer.class));
                followedListResponse.setPageTotal((Integer) resp.getJsonValue("pageTotal", Integer.class));
                followedListResponse.setFollowed(JSON.parseArray(resp.getJsonValue("followed"), Recall.class));
                callback.onSuccess(followedListResponse);
            }
        });

        jsonRequest.setTag(METHOD_GET_FOLLOW);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

    /**
     * 查询（搜索）回访记录[内容、疾病、次数，至少有一个]
     * @param content 回访内容
     * @param disease 疾病名称
     * @param times 回访次数
     * @param pageInfo 分页信息
     * @param callback
     */
    public static void searchFollowed(int isFollowed, String content, String disease, int times, PageInfo pageInfo,
                                      final ResultCallback<FollowedListResponse> callback) {
        FollowedSearchRequest followedSearchRequest = new FollowedSearchRequest();
        followedSearchRequest.setIsFollowed(isFollowed);
        followedSearchRequest.setContent(content);
        followedSearchRequest.setDisease(disease);
        followedSearchRequest.setTimes(times);
        followedSearchRequest.setPageInfo(pageInfo);

        ReqBean reqBean = ReqBean.newInstance(METHOD_Search_Followed, followedSearchRequest);
        String jsonParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                AssResponse resp = JSON.parseObject(response, AssResponse.class);
                FollowedListResponse followedListResponse = new FollowedListResponse();
                followedListResponse.setPageTotal(JSON.parseObject(resp.getJsonValue("pageTotal"), Integer.class));
                followedListResponse.setFollowed(JSON.parseArray(resp.getJsonValue("followed"), Recall.class));
                callback.onSuccess(followedListResponse);
            }
        });

        jsonRequest.setTag(METHOD_Search_Followed);
        AssistAPI.getRequestQueue().add(jsonRequest);

    }

    /**
     * 添加回访记录
     * @param addF
     * @param callback
     */
    public static void addFollowed(AddFollowedRequest addF, final ResultCallback<String> callback) {
        ReqBean reqBean = ReqBean.newInstance(METHOD_Add_Followed, addF);
        String jsonParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                callback.onSuccess("添加成功");
            }
        });

        jsonRequest.setTag(METHOD_Add_Followed);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }
}
